﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Ehai.Web.Framework.Rules
{
    class DNCOnlineRuleHandle : IMsgRuleHandle
    {
        #region IMsgRuleHandle 成员

        public const string Name = "dnconline";

#if DEBUG
        private int _execStatus = 0;

        public int ExecStatus
        {
            get { return _execStatus; }
        }
#endif
        /// <summary>
        /// 执行操作，将信息一字符串数组的形式传入，然后按照参数完成相应操作，使得本地目录与svn服务器目录一致
        /// </summary>
        /// <param name="p">传入的字符串数组</param>
        /// <param name="ci">ContextInfo对象</param>
        /// <returns>将结果返回到二维数组里，若成功，返回success，若失败，则是由传入参数不对引起的，抛出异常，不会返回二维数组</returns>
        public string[][] Execute(string[] p, ContextInfo ci)
        {
            string[][] result = { new string[1] { string.Empty } };
            if (p.Length < 4)
            {
#if DEBUG
                _execStatus = -2;
#endif
                return null;
            }
            try
            {
                for (int n = 0; n < 4; n++)
                {
                    if (String.IsNullOrEmpty(p[n])&&n!=2)
                    {
#if DEBUG
                        _execStatus = n;
#endif
                        string message = GetParametersInfo()[n] + "不能为空";
                        throw new Exception("");
                    }
                }
                string localpath = p[1];
                string svnpath = p[3];
                string svnurl = p[2];
                string username = (p.Length < 5) ? String.Empty : p[4].Trim();
                string password = (p.Length < 6) ? String.Empty : p[5].Trim();
                if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password))
                {
                    ProcessStartInfo psi = new ProcessStartInfo("cmd");
                    psi.RedirectStandardOutput = true;
                    psi.RedirectStandardInput = true;
                    psi.UseShellExecute = false;
                    psi.CreateNoWindow = true;
                    Process pro = Process.Start(psi);
                    pro.StandardInput.WriteLine(@"chdir /d  " + svnpath);
                    pro.StandardInput.WriteLine("svn commit -m \" " + System.DateTime.Now.ToString() + "\" --username " + username + " --password " + password + " " + localpath);//提交(上传)目录下面的改变文件
                    result[0][0] = "Commit success!";
                    pro.StandardInput.WriteLine(@"exit");
                    pro.WaitForExit();
                    pro.Close();
                    return result;
                }
                else if (String.IsNullOrEmpty(svnurl))
                {
                    ProcessStartInfo psi = new ProcessStartInfo("cmd");
                    psi.RedirectStandardOutput = true;
                    psi.RedirectStandardInput = true;
                    psi.UseShellExecute = false;
                    psi.CreateNoWindow = true;
                    Process pro = Process.Start(psi);
                    pro.StandardInput.WriteLine(@"chdir /d " + svnpath);
                    //更新(下载)服务器上的最新版本       
                    pro.StandardInput.WriteLine("svn update " + localpath);
                    result[0][0] = "Update success!";
                    pro.StandardInput.WriteLine(@"exit");
                    pro.WaitForExit();
                    pro.Close();
                    return result;
                }
                else
                {
                    if (String.IsNullOrEmpty(p[2])) throw new Exception("在进行checkout操作的时候，" + GetParametersInfo()[2] + "不能为空");
                    ProcessStartInfo psi = new ProcessStartInfo("cmd");
                    psi.RedirectStandardOutput = true;
                    psi.RedirectStandardInput = true;
                    psi.UseShellExecute = false;
                    psi.CreateNoWindow = true;
                    Process pro = Process.Start(psi);
                    pro.StandardInput.WriteLine(@"chdir /d  " + svnpath);
                    pro.StandardInput.WriteLine("svn checkout " + svnurl + " " + localpath);//提交(上传)目录下面的改变文件
                    result[0][0] = "Checkout success!";
                    pro.StandardInput.WriteLine(@"exit");
                    pro.WaitForExit();
                    pro.Close();
                    return result;
                }
                /*switch (p[0])
                {
                    case "1":
                        {
                            for (int i = 4; i <= 5; i++)
                            {
                                if (String.IsNullOrEmpty(p[i]))
                                {
                                    throw new Exception("在进行commit操作时，" + GetParametersInfo()[i] + "不能为空");
                                }
                            }
                            ProcessStartInfo psi = new ProcessStartInfo("cmd");
                            psi.RedirectStandardOutput = true;
                            psi.RedirectStandardInput = true;
                            psi.UseShellExecute = false;
                            psi.CreateNoWindow = true;
                            Process pro = Process.Start(psi);
                            pro.StandardInput.WriteLine(@"chdir /d  " + svnpath);
                            pro.StandardInput.WriteLine("svn commit -m \" " + System.DateTime.Now.ToString() + "\" --username " + username + " --password " + password + " " + localpath);//提交(上传)目录下面的改变文件
                            result[0][0] = "Commit success!";
                            pro.StandardInput.WriteLine(@"exit");
                            pro.WaitForExit();
                            pro.Close();
                            return result;
                        }
                    case "2":
                        {
                            ProcessStartInfo psi = new ProcessStartInfo("cmd");
                            psi.RedirectStandardOutput = true;
                            psi.RedirectStandardInput = true;
                            psi.UseShellExecute = false;
                            psi.CreateNoWindow = true;
                            Process pro = Process.Start(psi);
                            pro.StandardInput.WriteLine(@"chdir /d " + svnpath);
                            //更新(下载)服务器上的最新版本       
                            pro.StandardInput.WriteLine("svn update " + localpath);
                            result[0][0] = "Update success!";
                            pro.StandardInput.WriteLine(@"exit");
                            pro.WaitForExit();
                            pro.Close();
                            return result;
                        }
                    case "3":
                        {
                            if (String.IsNullOrEmpty(p[2])) throw new Exception("在进行checkout操作的时候，" + GetParametersInfo()[2] + "不能为空");
                            ProcessStartInfo psi = new ProcessStartInfo("cmd");
                            psi.RedirectStandardOutput = true;
                            psi.RedirectStandardInput = true;
                            psi.UseShellExecute = false;
                            psi.CreateNoWindow = true;
                            Process pro = Process.Start(psi);
                            pro.StandardInput.WriteLine(@"chdir /d  " + svnpath);
                            pro.StandardInput.WriteLine("svn checkout " + svnurl + " " + localpath);//提交(上传)目录下面的改变文件
                            result[0][0] = "Checkout success!";
                            pro.StandardInput.WriteLine(@"exit");
                            pro.WaitForExit();
                            pro.Close();
                            return result;
                        }
                    default:
                        {
                            throw new Exception("操作信息输入有误");
                        }
                }*/
            }
            catch (Exception ex)
            {
#if DEBUG
                // 操作成功,状态值
                _execStatus = -1;
#endif
                throw new Ehai.Web.Framework.WebException("Ehai.Web.DNCOnline", "svn操作异常！", ex);
            }
        }

        public string GetHelpInfo()
        {
            return "将本地特定目录与svn服务器目录保持一致";
        }

        public string GetHelpDetail()
        {
            string help1 = "如果你要执行commit操作，需要传入操作号(\"dnconline\")、本地文件目录、svn.exe所在的目录、用户名、密码，例如{1,e:\\svn,c:\\Subversion\bin,anderson,anderson}</br>";
            string help2 = "如果你要执行update操作，需要传入操作号(\"dnconline\")、本地文件目录、svn.exe所在的目录，例如{2,e:\\svn,c:\\Subversion\bin}</br>";
            string help3 = "如果你要执行checkout操作，需要传入操作号(\"dnconline\")、本地文件目录、svn链接、svn.exe所在的目录，例如{3,e:\\svn,svn://202.204.118.27/project/test,c:\\Subversion\bin}";
            return help1 + help2 + help3;
        }

        public string[] GetParametersInfo()
        {
            string[] info = new string[4];
            info[0] = "(必选)操作号，必须填dnconline";
            info[1] = "(必选)本地文件的目录";
            info[2] = "(可选)svn链接的地址";
            info[3] = "(必选)svn.exe文件所在的目录名";
            info[4] = "(可选)用户名";
            info[5] = "(可选)密码";
            return info;
        }

        #endregion
    }
}
